package com.vren.addressParse.modules.area;

import com.alibaba.fastjson.JSONObject;
import com.vren.addressParse.common.enums.AreaEnum;
import com.vren.addressParse.modules.addressParse.domain.address.Format;
import com.vren.addressParse.modules.area.domain.entity.AreaEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @Description:
 * @Author: 张文Uncle
 * @Date: 2021-07-27 15:39
 **/
@Service
public class AreaService {

    private Map<Long, AreaEntity> province;
    private Map<Long, AreaEntity> city;
    private Map<Long, AreaEntity> area;
    private Map<Long, AreaEntity> street;

    private Map<Long, String> streetsByProvinceCode = new HashMap<>();
    private Map<Long, String> streetsByCityCode = new HashMap<>();
    private Map<Long, String> streetsByAreaCode = new HashMap<>();

    @Autowired
    private AreaMapper areaMapper;

    @PostConstruct
    private void initAddress() {
        List<AreaEntity> list = areaMapper.selectList(null);
        Map<Integer, Map<Long, AreaEntity>> result = list.stream()
                .collect(Collectors.groupingBy(AreaEntity::getLevel, Collectors.toMap(AreaEntity::getCode, Function.identity())));
        province = result.get(AreaEnum.PROVINCE.getCode());
        city = result.get(AreaEnum.CITY.getCode());
        area = result.get(AreaEnum.AREA.getCode());
        street = result.get(AreaEnum.STREET.getCode());
        street.values().stream().collect(Collectors.groupingBy(AreaEntity::getProvince)).forEach((index, item) -> streetsByProvinceCode.put(index, JSONObject.toJSONString(
                item.stream()
                        .map(province -> new Format(province.getCode(), String.format("%s，%s，%s", province.getName(), province.getErrorMapping(), province.getCustomMapping()), province.getProvince(), province.getCity(), province.getArea()))
                        .collect(Collectors.toList()))
        ));
        street.values().stream().collect(Collectors.groupingBy(AreaEntity::getCity)).forEach((index, item) -> streetsByCityCode.put(index, JSONObject.toJSONString(
                item.stream()
                        .map(city -> new Format(city.getCode(), String.format("%s，%s，%s", city.getName(), city.getErrorMapping(), city.getCustomMapping()), city.getProvince(), city.getCity(), city.getArea()))
                        .collect(Collectors.toList()))
        ));
        street.values().stream().collect(Collectors.groupingBy(AreaEntity::getArea)).forEach((index, item) -> streetsByAreaCode.put(index, JSONObject.toJSONString(
                item.stream()
                        .map(city -> new Format(city.getCode(), String.format("%s，%s，%s", city.getName(), city.getErrorMapping(), city.getCustomMapping()), city.getProvince(), city.getCity(), city.getArea()))
                        .collect(Collectors.toList()))
        ));
        System.out.println(areaMapper);
    }

    public List<AreaEntity> getProvinceList() {
        return new ArrayList<>(province.values());
    }

    public List<AreaEntity> getCityList() {
        return new ArrayList<>(city.values());
    }

    public List<AreaEntity> getAreaList() {
        return new ArrayList<>(area.values());
    }

    public List<AreaEntity> getStreetList() {
        return new ArrayList<>(street.values());
    }

    public String getStreetsByProvinceCode(Long province) {
        return streetsByProvinceCode.get(province);
    }

    public String getStreetsByCityCode(Long city) {
        return streetsByCityCode.get(city);
    }

    public String getStreetsByAreaCode(Long area) {
        return streetsByAreaCode.get(area);
    }
}
